import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useTasklistStore = defineStore('tasklist', () => {
  var tasklist = ref([]);

  //未完成的任务数量
  var unFinishedCount = computed(() => {
    return tasklist.value.filter((item) => !item.status).length;
  })
  //新增任务
  var addTask = (payload) => {
    tasklist.value.push({ id: tasklist.value.length + 1, name: payload, status: false });
  }
  //更改任务状态
  var changeStatus = (payload) => {
    var task = tasklist.value.find((item) => {
      return item.id == payload;
    })
    if (task) {
      task.status = !task.status;
    }
  }
  //删除任务
  var deleteTask = (payload) => {
    var index = tasklist.value.findIndex((item) => {
      return item.id == payload;
    })
    if (index != -1) {
      tasklist.value.splice(index, 1);
    }
  }

  return { tasklist, unFinishedCount, addTask, changeStatus, deleteTask }
})
